package sel.me.spring.boot245.web.domain.base;


import org.apache.commons.lang3.StringUtils;
import sel.me.java.learning.common.base.entity.BaseEntity;
import sel.me.spring.boot245.web.exception.UnifyStrErrorCode;

import java.io.Serializable;

public class UnifyResult<T> extends BaseEntity implements Serializable {

	private static final long serialVersionUID = -9075544910442561812L;
	private String code;
	private String msg;
	private String causeApp;
	private String exceptionType;
	private T result;

	public UnifyResult() {
		code = UnifyStrErrorCode.SUCCESS.getErrorCode();
		msg = UnifyStrErrorCode.SUCCESS.getErrorMsg();
	}

	public UnifyResult(T result) {
		code = UnifyStrErrorCode.SUCCESS.getErrorCode();
		msg = UnifyStrErrorCode.SUCCESS.getErrorMsg();
		this.result = result;
	}

	public UnifyResult(String code, String msg) {
		this.code = UnifyStrErrorCode.SUCCESS.getErrorCode();
		this.msg = UnifyStrErrorCode.SUCCESS.getErrorMsg();
		this.code = code;
		this.msg = msg;
	}

	public UnifyResult(String code, String msg, T result) {
		this.code = UnifyStrErrorCode.SUCCESS.getErrorCode();
		this.msg = UnifyStrErrorCode.SUCCESS.getErrorMsg();
		this.code = code;
		this.msg = msg;
		this.result = result;
	}

	public String getCauseApp() {
		return causeApp;
	}

	public void setCauseApp(String causeApp) {
		this.causeApp = causeApp;
	}

	public UnifyResult<T> setError(String code, String msg) {
		this.code = code;
		this.msg = msg;
		return this;
	}

	public UnifyResult<T> setError(UnifyStrErrorCode unifyStrErrorCode) {
		code = unifyStrErrorCode.getErrorCode();
		msg = unifyStrErrorCode.getErrorMsg();
		return this;
	}

	public T getResult() {
		return result;
	}

	public UnifyResult<T> setResult(T result) {
		this.result = result;
		return this;
	}


	public boolean isSuccess() {
		return StringUtils.equals(code, UnifyStrErrorCode.SUCCESS.getErrorCode());
	}

	public String getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}


	public UnifyResult<T> setCode(String code) {
		this.code = code;
		return this;
	}

	public UnifyResult<T> setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public static UnifyResult create(String code, String msg, Object data) {
		return new UnifyResult(code, msg, data);
	}

	public static UnifyResult create(String code, String msg) {
		return new UnifyResult(code, msg);
	}

	public static UnifyResult create(Object data) {
		if (data != null && (data instanceof UnifyStrErrorCode)){
			return new UnifyResult(((UnifyStrErrorCode) data).getErrorCode(), ((UnifyStrErrorCode) data).getErrorMsg());
		}else{
			return new UnifyResult(data);
		}

	}

}
